/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package war;

import com.google.gdata.client.Query;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Giuseppe
 */


public class ManageCalendar {
    private CalendarService myService;
    private List<CalendarEventEntry> allCalEntries;
  /*  public ManageCalendar()  throws Exception{
    
        myService=creaConnessione(myService);
         myService.getFeed(new URL("https://www.google.com/calendar/feeds/default/allcalendars/full"), CalendarFeed.class);
          allCalEntries = trovaEventiBetweenDate1("One-Hit Kills Release Date Calender", "2014-12-31T23:59:59");
  
             
    } */
    public ManageCalendar(String nameCalendar)  throws Exception{
    
        myService=creaConnessione(myService);
        myService.getFeed(new URL("https://www.google.com/calendar/feeds/default/allcalendars/full"), CalendarFeed.class);
        allCalEntries = trovaEventiBetweenDate1(nameCalendar, "2014-12-31T23:59:59");
  
             
    }   
     public static CalendarService creaConnessione(CalendarService service) throws MalformedURLException,IOException, AuthenticationException {
        String login ="ultratoldo@gmail.com";//
        String password = "interista"+("GK33LKK7JLGJ3DLE9GEEGM9R7G39GJGM9R7G3CA4GK3C4J9E4rweGK3AHFH6CK").length();


        // Crea una connessione
        service = new CalendarService("exampleCo-exampleApp-1");
        service.setUserCredentials(login, password);

        //reperisce e stampa tutti i titoli dei miei calendari

        return service;
    }
  public Date getDateVideogame(String name) throws Exception{
    
        for (CalendarEventEntry eventEntry:allCalEntries ) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//e non mm minuscule che rappresentano minuti
            Date d = null;
            if(eventEntry.getTitle().getPlainText().toLowerCase().equals(name.toLowerCase())){
                List<When> dates=eventEntry.getTimes();
                for(When date:dates){
                    d=dateFormat.parse(date.getEndTime().toString()); 
                }
                return d;
            }
        }
        return null;




}

public String getCalendarId(String title) throws Exception {
        URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
        CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
        String id = "";
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            CalendarEntry entry = resultFeed.getEntries().get(i);
            if (entry.getTitle().getPlainText().equals(title)) {
                //http://www.google.com/support/forum/p/apps-apis/thread?tid=2cdc8238c5066e7c&hl=en
                id = entry.getLink("http://schemas.google.com/gCal/2005#eventFeed", null).getHref();
                return id;
            }
        }
        return null;
    }
public  void inserisciEvento(String gameName, String calendarname) throws Exception {
        //URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
        //CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
        URL postUrl = new URL(this.getCalendarId(calendarname));
        //Creo l'evento e lo personalizzo
        CalendarEventEntry myEntry = new CalendarEventEntry();

        myEntry.setTitle(new PlainTextConstruct(gameName));
       // myEntry.setContent(new PlainTextConstruct("Meet for a quick lesson."));

        DateTime startTime = DateTime.parseDateTime("2012-12-05T15:00:00-08:00");
        DateTime endTime = DateTime.parseDateTime("2013-06-18T17:00:00-08:00");
        When eventTimes = new When();
        //eventTimes.setStartTime(startTime);
       // eventTimes.setEndTime(endTime);
        //myEntry.addTime(eventTimes);

        // Inserisco l'evento
        //CalendarEventEntry insertedEntry = myService.insert(postUrl, myEntry);
    }

    public  void eliminaEvento(String gameName, String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));
        //creo una query e ottengo la lista di eventi che la soddisfano nel calendario
        Query myQuery = new Query(feedUrl);
        myQuery.setFullTextQuery(gameName);
        CalendarEventFeed myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);
        if (myResultsFeed.getEntries().size() > 0) {
            CalendarEventEntry firstMatchEntry = (CalendarEventEntry) myResultsFeed.getEntries().get(0);
            String myEntryTitle = firstMatchEntry.getTitle().getPlainText();
            //System.out.println("==="+myEntryTitle);
            firstMatchEntry.delete();
        }

    }
public  CalendarEventFeed trovaEventi(String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));
        //creo una query e ottengo la lista di eventi che la soddisfano nel calendario
        Query myQuery = new Query(feedUrl);
        myQuery.setMaxResults(5000);
         return myService.query(myQuery, CalendarEventFeed.class);
}
public  List<CalendarEventEntry> trovaEventi2(String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);

        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }
public List<CalendarEventEntry> trovaEventiBetweenDate1(String calendarname, String data) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);
        DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
        Date dt = Calendar.getInstance().getTime();
        myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        myQuery.setMaximumStartTime(DateTime.parseDateTime(data));
        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }
public List<CalendarEventEntry> trovaEventiBetweenDate2(String calendarname, String data1, String data2) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);
        //DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
        //Date dt = Calendar.getInstance().getTime();
        /*DateFormat format =
            DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
            DateFormat.SHORT);
        Date d1 = format.parse(data1);
        Date d2 = format.parse(data2);
        myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(d1)));
        myQuery.setMaximumStartTime(DateTime.parseDateTime(dfGoogle.format(d2)));*/
        myQuery.setMinimumStartTime(DateTime.parseDateTime(data1));
        myQuery.setMaximumStartTime(DateTime.parseDateTime(data2));
        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }
public List<CalendarEventEntry> trovaEventiDelGiorno(String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);
        //DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
        Date dt = Calendar.getInstance().getTime();
        //myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        //myQuery.setMaximumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        if((dt.getMonth()) < 10){
            if((dt.getDate()) < 10){
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T23:59:59"));
            }
            else {
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+dt.getDate()+"T23:59:59"));
            }
        }
        else{
            if((dt.getDate()) < 10){
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T23:59:59"));
            }
            else {
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate()+"T23:59:59"));
            }
        }
        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }
public List<CalendarEventEntry> trovaEventiDellaSettimana(String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);
        //DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
        Date dt = Calendar.getInstance().getTime();
        //myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        //myQuery.setMaximumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        int giorno = dt.getDate() + 6;
        int mese = dt.getMonth()+1;
        if(mese < 10){
            if((dt.getDate()) < 10){
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T00:00:01"));
                if(giorno < 10) myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-0"+giorno+"T23:59:59"));
                else myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+giorno+"T23:59:59"));
            }   
            else if((dt.getDate()) < 26) {
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+giorno+"T23:59:59"));
            }
            else{
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                giorno -= 31;
                mese += 1;
                if(mese < 10) myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+mese+"-0"+giorno+"T23:59:59"));
                else myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+mese+"-0"+giorno+"T23:59:59"));
            }
        }
        else {
            if((dt.getDate()) < 10){
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-0"+dt.getDate()+"T00:00:01"));
                if(giorno < 10) myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-0"+giorno+"T23:59:59"));
                else myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+giorno+"T23:59:59"));
            }   
            else if((dt.getDate()) < 26) {
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+giorno+"T23:59:59"));
            }
            else{
                myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate()+"T00:00:01"));
                giorno -= 31;
                mese += 1;
                if(1 < mese) myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+mese+"-0"+giorno+"T23:59:59"));
                else myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+mese+"-0"+giorno+"T23:59:59"));
            }
        }
        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }
public List<CalendarEventEntry> trovaEventiDelMese(String calendarname) throws Exception {
        URL feedUrl = new URL(this.getCalendarId(calendarname));

        CalendarQuery myQuery = new CalendarQuery(feedUrl);
        //DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
        Date dt = Calendar.getInstance().getTime();
        //myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        //myQuery.setMaximumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
        if((dt.getMonth()) < 10){
            myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-01"+"T00:00:01"));
            myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-0"+(dt.getMonth()+1)+"-31"+"T23:59:59"));
        }
        else{
            myQuery.setMinimumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-01"+"T00:00:01"));
            myQuery.setMaximumStartTime(DateTime.parseDateTime((dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-31"+"T23:59:59"));
        }
        
        myQuery.setMaxResults(5000);
        int startIndex = 1;
        int entriesReturned;
        CalendarEventFeed myResultsFeed;
        List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();

        while (true) {
            myQuery.setStartIndex(startIndex);
            myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

            entriesReturned = myResultsFeed.getEntries().size();
            if (entriesReturned == 0)
                break;

            allCalEntries.addAll(myResultsFeed.getEntries());
            startIndex = startIndex + entriesReturned;
        }
        return allCalEntries;
    }


    
}
